package designPattern.singleton;

import java.io.*;

/*
序列化破坏单例测试
 */
public class TestSerializableSingleton {
    public static void main(String[] args) {
        SerializableSingleton ss1 = SerializableSingleton.getInstance();
        SerializableSingleton ss2 = null;

        FileOutputStream fos;
        try {
            fos = new FileOutputStream("SerializableSingleton.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(ss1);
            oos.flush();
            oos.close();

            FileInputStream fis = new FileInputStream("SerializableSingleton.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);
            ss2 = (SerializableSingleton) ois.readObject();
            ois.close();
            System.out.println(ss1);
            System.out.println(ss2);
            System.out.println(ss1 == ss2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
